/** <!--------------------------------------------------------------------------------------------->
 * @file RevGraphItemLink.h
 *
 * GSvn - A Graphical Client for SVN
 * Copyright (C) 2012  Karl Krach, gsvn@bluespirit.la
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 <!---------------------------------------------------------------------------------------------> */

#ifndef REVGRAPHITEMLINK_H
#define REVGRAPHITEMLINK_H


// -- includes --
#include <QMap>
#include <QDebug>

// --- forward declarations ---
class RevGraphItem;
class QGraphicsItem;

// --- namespaces ---
// --- defines ---


/** <!--------------------------------------------------------------------------------------------->
 * Object which links all graph items together (map iterator).
 *
 * @author	Karl Krach, <gsvn@bluespirit.la>
 * @since	2012-02-11
 <!---------------------------------------------------------------------------------------------> */
class RevGraphItemLink : public QMap<int, RevGraphItem*>::iterator
{
	/* Friends */
	friend class RevGraphItemMap;


	/* Con-/Destructors */
public:

	/** <!----------------------------------------------------------------------------------------->
	 * Creates a RevGraphItemLink object from another RevGraphItemLink object
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink( const RevGraphItemLink& rOther )
	    : QMap<int, RevGraphItem*>::iterator( rOther ), m_pMap( rOther.m_pMap )
	{
	}


	/* Con-/Destructors */
private:

	/** <!----------------------------------------------------------------------------------------->
	 * Creates a RevGraphItemLink object
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink() : QMap<int, RevGraphItem*>::iterator(), m_pMap(0)
	{
	}

	/** <!----------------------------------------------------------------------------------------->
	 * Creates a RevGraphItemLink object from an iterator position and a pointer to the map
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink( const QMap<int, RevGraphItem*>::iterator& rOther,
	                  QMap<int, RevGraphItem*>* pMap )
	    : QMap<int, RevGraphItem*>::iterator( rOther ), m_pMap( pMap )
	{
	}


	/* Methods */
public:

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true, when there is an item with the given ID
	 <!-----------------------------------------------------------------------------------------> */
	bool hasItem( int iRevID ) { return m_pMap && m_pMap->contains( iRevID ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true, when there is a successor object in the map
	 <!-----------------------------------------------------------------------------------------> */
	bool hasNext() const { return m_pMap && (*this)+1 != m_pMap->end(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns true when there is a predecessor object in the map
	 <!-----------------------------------------------------------------------------------------> */
	bool hasPrevious() const { return m_pMap && (*this) != m_pMap->begin(); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the item link of the given ID
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink getItem( int iRevID )
	{
		return RevGraphItemLink( m_pMap->find(iRevID), m_pMap );
	}

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the previous item link
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink getPrevious() const { return RevGraphItemLink( (*this)-1, m_pMap ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all previous graphics items
	 <!-----------------------------------------------------------------------------------------> */
	QList<QGraphicsItem *> getAllPrevious();

	/** <!----------------------------------------------------------------------------------------->
	 * Returns the next item link
	 <!-----------------------------------------------------------------------------------------> */
	RevGraphItemLink getNext() const { return RevGraphItemLink( (*this)+1, m_pMap ); }

	/** <!----------------------------------------------------------------------------------------->
	 * Returns a list of all following graphics items
	 <!-----------------------------------------------------------------------------------------> */
	QList<QGraphicsItem *> getAllNext();


	/* Members */
private:

	/** <!----------------------------------------------------------------------------------------->
	 * Pointer to the map (where the iterator points to). Needed to implement the hasNext() and
	 * hasPrevious() methods.
	 <!-----------------------------------------------------------------------------------------> */
	QMap<int, RevGraphItem*>* m_pMap;

};	// END class RevGraphItemLink


#endif // REVGRAPHITEMLINK_H
